package Thread_;

public class Thread11 {
    private static long count;

    public static void main4(String[] args) throws InterruptedException {
        Thread t=new Thread(()->{
            for(long i=0;i<100_0000_0000L;i++){
                count+=i;
            }
        });
        t.start();
        long beg=System.currentTimeMillis();
        t.join();
        long end=System.currentTimeMillis();
        System.out.println("count= "+count);
        System.out.println("运算时间为： "+(end-beg));

    }

    public static void main(String[] args) throws InterruptedException {
        //计算1--100_0000_0000(100亿)相加,分成两个线程执行,再在main线程中打印结果
        Thread t1=new Thread(()->{
            long n=0;
            //t1: 计算1到5000的相加
            for(long i=1;i<=500_0000_0000L;i++){
                n+=i;
            }
            count+=n;
            System.out.println("Thread线程执行结束!");
        });
        Thread t2=new Thread(()->{
            long n=0;
            for(long i=500_0000_0001L;i<=1000_0000_0000L;i++){
                n+=i;
            }
            count+=n;
            System.out.println("Thread线程执行结束!");
        });
        t1.start();
        t2.start();
        long beg=System.currentTimeMillis();

        t1.join();//线程等待
        t2.join();//线程等待
        long end=System.currentTimeMillis();
        System.out.println("结果为: "+count);
        System.out.println("运算时间为： "+(end-beg));
    }

    public static void main1(String[] args) throws InterruptedException {
        //计算1--5相加,再在main线程中打印结果
        Thread t=new Thread(()->{
            for(int i=0;i<5;i++){
                count+=i;
            }
        System.out.println("Thread线程执行结束!");
        });
     t.start();
     t.join();//线程等待
     System.out.println("结果为: "+count);
    }
}
